# -*- coding: utf-8 -*-
# @Time    : 2024/03/06 17:35
# @Author  : Mr.su
# @FileName: methods.py
# @FileDesc: 

import hashlib, random


def make_md5(data):
    # 生成md5方法
    if isinstance(data, str): data = data.encode("utf-8")
    m = hashlib.md5()
    m.update(data)
    return m.hexdigest()


def random_str(length):
    # 生成8位随机数字
    return ''.join([random.choice('0123456789') for i in range(length)])


def is_contain_chinese(name):
    # 判断名称是否包含中文
    result_lis = [True for i in name if u'\u4e00' <= i <= u'\u9fff']
    return True if result_lis else False


def make_img_name(product_name, estr, brand, isfirst=False, num=None):
    # 生成图片名称
    if is_contain_chinese(product_name):
        file_name = '{}-{}-{}_{}.jpg'.format(brand, product_name, estr, num) if not isfirst else '{}-{}-{}.jpg'.format(brand, product_name, estr)
    else:
        file_name = '{}-{}_{}.jpg'.format(brand, estr, num) if not isfirst else '{}-{}.jpg'.format(brand, estr)
    return file_name
